#include "utils.h"
#include <time.h>

void cg_srand(void)
{
    srand(time(NULL));
}

double cg_frand01(void)
{
    return ((double)rand() / (RAND_MAX + 1.0));
}

double cg_frand(double l, double r)
{
    return (l + cg_frand01() * (r - l));
}

void cg_random_points(cgPoint *dest, int n, 
		      double xl, double xr,
		      double yl, double yr,
		      double zl, double zr)
{
    int i;
    for (i = 0; i < n; i++)
    {
	dest[i].c[0] = cg_frand(xl, xr);
	dest[i].c[1] = cg_frand(yl, yr);
	dest[i].c[2] = cg_frand(zl, zr);
    }
}

void cg_random_lines(cgLine *dest, int n, 
		     double xl, double xr,
		     double yl, double yr,
		     double zl, double zr)
{
    int i;
    for (i = 0; i < n; i++)
	cg_random_points(dest[i].p, 2, xl, xr, yl, yr, zl, zr);
}

void cg_random_triangles(cgTriangle *dest, int n,
			 double xl, double xr,
			 double yl, double yr,
			 double zl, double zr)
{
    int i;
    for (i = 0; i < n; i++)
	cg_random_points(dest[i].p, 3, xl, xr, yl, yr, zl, zr);
}

void cg_random_grid(cgPoint **dest, int w, int h,
		    double xl, double xr,
		    double yl, double yr,
		    double zl, double zr)
{
    double xstep = (xr - xl) / (w - 1);
    double ystep = (yr - yl) / (h - 1);
    double currx, curry;
    int i, j;

    curry = yl;
    for (i = 0; i < h; i++)
    {
	currx = xl;
	for (j = 0; j < w; j++)
	{
	    dest[i][j].c[0] = currx;
	    dest[i][j].c[1] = curry;
	    dest[i][j].c[2] = cg_frand(zl, zr);
	    currx += xstep;
	}
	curry += ystep;
    }
}
